package com.yuqing.sentimentapp.home.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseFragment;
import com.yuqing.sentimentapp.channel.ChannelActivity;
import com.yuqing.sentimentapp.channel.ChannelData;
import com.yuqing.sentimentapp.home.acitivity.SearchActivity;
import com.yuqing.sentimentapp.home.adapter.HomeViewPagerAdapter;
import com.yuqing.sentimentapp.home.bean.HotChannelBean;
import com.yuqing.sentimentapp.login.bean.LoginResultBean;
import com.yuqing.sentimentapp.utils.SharePrefrenceHelper;
import com.yuqing.sentimentapp.widget.jzvideo.Jzvd;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment{

    public static final int RESULT_CODE = 100;
    public static final int RESULT_CODE_SEARCH = 200;
    public static final String RESULT_CHANNEL_DATA = "result_channel_data";
    public static final String RESULT_CHANNEL_POSITION = "result_channel_position";

    @BindView(R.id.tabs)
    TabLayout mTabs;
    @BindView(R.id.iv_other)
    ImageView mIvOther;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    private List<ChannelData> mChannelList = new ArrayList<>();
    private HomeViewPagerAdapter mAdapter;
    private List<ChannelData> channelList;
    private int selectChannelPos;
    private String selectChannelId;

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        initChannel();
        setViewPager();
    }

    private void initChannel() {
        LoginResultBean loginResultBean = SharePrefrenceHelper.getLoginResultBean();
        if (loginResultBean == null) return;
        LoginResultBean.DataBean data = loginResultBean.getData();
        if (data == null) return;
        List<ChannelData> userCategory = data.getUserCategory();
        if (userCategory == null) return;
        for (int i = 0; i < userCategory.size(); i++) {
            if (TextUtils.equals("1", userCategory.get(i).getStatus())) {//我的栏目
                mChannelList.add(userCategory.get(i));
            }
        }
    }

    protected void initPresenter() {

    }

    /**
     * 填充ViewPager内容
     */
    private void setViewPager() {
        mAdapter = new HomeViewPagerAdapter(getChildFragmentManager(), mChannelList);
        mViewPager.setAdapter(mAdapter);
        mTabs.setTabTextColors(getResources().getColor(R.color.color_aaaaaa), getResources().getColor(R.color.black));
        mTabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.custom_tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextColor(mTabs.getTabTextColors());
                textView.setTextSize(16);
                textView.setTypeface(Typeface.DEFAULT_BOLD);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.custom_tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextSize(15);
                textView.setTypeface(Typeface.DEFAULT);

                //释放视频
                Jzvd.resetAllVideos();
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mTabs.setupWithViewPager(mViewPager);
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        super.onFragmentVisibleChange(isVisible);

    }
    @OnClick({R.id.iv_other,R.id.ll_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_other:
                turnToChannelPage();
                break;
            case R.id.ll_search:
                turnToSearchPage();
                break;
            default:
                break;
        }
    }

    private void turnToChannelPage() {
        Intent intent = new Intent(getActivity(), ChannelActivity.class);
        intent.putExtra("selectId", mChannelList.get(mTabs.getSelectedTabPosition()).getCategoryId());
        startActivityForResult(intent, RESULT_CODE);
    }

    private void turnToSearchPage() {
        Intent intent = new Intent(getActivity(), SearchActivity.class);
        startActivityForResult(intent, RESULT_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_CODE) {
            if (resultCode == RESULT_CODE) {
                channelList = (ArrayList<ChannelData>) data.getSerializableExtra(RESULT_CHANNEL_DATA);
                selectChannelId = data.getStringExtra(RESULT_CHANNEL_POSITION);
                mChannelList.clear();
                mChannelList.addAll(channelList);
                setViewPager();
                for (int i = 0; i < mChannelList.size(); i++) {
                    if (TextUtils.equals(selectChannelId, mChannelList.get(i).getCategoryId())) {
                        selectChannelPos = i;
                        break;
                    }
                }
                //设置选中项
                TabLayout.Tab tabAt = mTabs.getTabAt(selectChannelPos);
                if (tabAt != null) {
                    tabAt.select();
                }
            } else if (resultCode == RESULT_CODE_SEARCH) {
                HotChannelBean.DataBean dataBean = (HotChannelBean.DataBean) data.getSerializableExtra(RESULT_CHANNEL_DATA);
                for (int i = 0; i < mChannelList.size(); i++) {
                    if (TextUtils.equals(dataBean.getId(), mChannelList.get(i).getCategoryId())) {
                        //设置选中项
                        TabLayout.Tab tabAt = mTabs.getTabAt(i);
                        if (tabAt != null) {
                            tabAt.select();
                        }
                        break;
                    }
                }
            }
        }
    }
}
